package com.showcai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.showcai.dao.ApplyWatchMapper;
import com.showcai.dao.DistributorMapper;
import com.showcai.dao.DistributorRelationMapper;
import com.showcai.def.Constant;
import com.showcai.module.ApplyWatch;
import com.showcai.module.Distributor;
import com.showcai.module.DistributorRelation;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.DistributorService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-7-31.
 */
@Service
public class DistributorServiceImpl implements DistributorService {

    @Autowired
    DistributorMapper distributorMapper;
    @Autowired
    DistributorRelationMapper distributorRelationMapper;

    private void modifyDistributor(Distributor distributor) {

        Long superiorId = distributor.getSuperiorId();
        DistributorRelation distributorRelation=new DistributorRelation();
        if (superiorId != null) {
            Distributor supDistributor = distributorMapper.getDistributorById(superiorId);
            if (supDistributor == null) {
                throw new MyRunTimeExcption("上级不存在！");
            }
            if (superiorId == distributor.getTid()) {
                throw new MyRunTimeExcption("不能成为自己的上级！");
            }
            distributorRelation.setDistributorId(distributor.getTid());
            distributorRelation.setSuperiorId(superiorId);
        }
        if (distributor.getTid() != null) {
            distributorMapper.updateByPrimaryKeySelective(distributor);
        return;
        }
        distributorMapper.insertDistributor(distributor);
        distributorRelation.setDistributorId(distributor.getTid());
        modifyDistributorRelation(distributorRelation);
    }

    private void modifyDistributorRelation(DistributorRelation distributorRelation) {

        if(distributorRelation.getTid()!=null){
            distributorRelationMapper.deleteById(distributorRelation.getTid());
        }
        if(distributorRelation.getSuperiorId()==null){
            distributorRelation.setSuperiorId(0l);
        }
        distributorRelation.setCreateTime(new Date());
        distributorRelationMapper.insertDistributorRelation(distributorRelation);
    }
    @Override
    public Distributor createDistributor(Distributor distributor) {


        modifyDistributor(distributor);
        return distributor;
    }

    @Override
    public Distributor getDistributorById(Long id) {
        return  distributorMapper.getDistributorById(id);
    }

    @Override
    public PageInfo distributorStatistics(Map<String, String> map) {
        String pageStr = map.get("page");
        String pageSizeStr = map.get("pageSize");
        Integer page = null;
        Integer pageSize = null;
        if (StringUtils.isNotBlank(pageStr) && StringUtils.isNumeric(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr) && StringUtils.isNumeric(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        if (null == page) {
            page = Constant.PAGENUM;
        }
        if (null == pageSize) {
            pageSize = Constant.PAGESIZENum;
        }

        PageHelper.startPage(page, pageSize);
        List<Map<String,String>> distributorStatisticsList = distributorMapper.distributorStatistics(map);
        PageInfo<Map<String,String>> pageinfo = new PageInfo<>(distributorStatisticsList);
        return pageinfo;
    }

    @Override
    public Map distributorStatisticsALl(Map<String, String> map) {
     return distributorMapper.distributorStatisticsALl(map);
    }

    @Override
    public PageInfo getDistributorList(Map<String, String> map) {
        String pageStr = map.get("page");
        String pageSizeStr = map.get("pageSize");
        Integer page = null;
        Integer pageSize = null;
        if (StringUtils.isNotBlank(pageStr) && StringUtils.isNumeric(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr) && StringUtils.isNumeric(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        if (null == page) {
            page = Constant.PAGENUM;
        }
        if (null == pageSize) {
            pageSize = Constant.PAGESIZENum;
        }


        PageHelper.startPage(page, pageSize);
        List<Distributor> distributorList = distributorMapper.getDistributorList(map);
        PageInfo<Distributor> pageinfo = new PageInfo<>(distributorList);
        return pageinfo;
    }

    @Override
    public List<Distributor> selectDistributorList(Map<String, String> map) {
        return distributorMapper.getDistributorList(map);
    }

    @Override
    public Distributor modifyDistributorInfo(Distributor distributor) {

if(distributor.getTid()==null){

    return createDistributor(distributor);
}
        modifyDistributor(distributor);

        return distributor;
    }

    @Override
    public Map<String,Object> getDistributionRequirements(String phone) {
        if(StringUtils.isBlank(phone)) {
            throw new MyRunTimeExcption("手机号为空！");
        }
        Map<String,Object> mapRes=new HashedMap();
        Map<String,String> map=new HashMap<>();
        map.put("phone",phone);
      List<Distributor> distributorList=  distributorMapper.getDistributionRequirements(map);
        mapRes.put("distributorList",distributorList);
        map.clear();
        if(distributorList!=null && distributorList.size()>0){

            map.put("superiorId",String.valueOf(distributorList.get(0).getTid()));
            List<Distributor> distributorSuperiorList=   distributorMapper.getDistributionRequirements(map);
            mapRes.put("distributorSuperiorList",distributorSuperiorList);
        }




        return mapRes;
    }
}
